package com.comunity_cloud.client.view2;

import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

import com.comunity_cloud.client.model.CommunityFile;
import com.comunity_cloud.client.model.CommunityFile.State;
import com.comunity_cloud.client.model.CommunityModel2;


public class ProgressCellRenderer implements TableCellRenderer {

	private Map<CommunityFile,JProgressBar> progressBars = new HashMap<CommunityFile, JProgressBar>();
	
	
		@Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			CommunityModel2 model = (CommunityModel2) table.getModel();
			CommunityFile file = model.getRow(row);
            int progress = 0;
            if (value instanceof Float) {
                progress = Math.round(((Float) value) * 100f);
            } else if (value instanceof Integer) {
                progress = (int) value;
            }
            if(file.getState() == State.SYNCHRONIZED) {
            	JLabel label = new JLabel("Synchronized");
            	label.setIcon(new ImageIcon(getClass().getResource("/images/check.png")));
            	label.setOpaque(true);
            	label.setBackground(table.getBackground());
            	label.setBorder(new EmptyBorder(0, 5, 0, 5));
            	if(isSelected) {
            		label.setBackground(table.getSelectionBackground());
            		label.setForeground(table.getSelectionForeground());            		
            	} else {
            		label.setForeground(table.getForeground());
        			label.setBackground(table.getBackground());
            	}
            	return label;
            } else if(file.getState() == State.PENDING){
            	JLabel label = new JLabel("Pending");
            	
            	label.setOpaque(true);
            	label.setBackground(table.getBackground());
            	label.setBorder(new EmptyBorder(0, 5, 0, 5));
            	if(isSelected) {
            		label.setBackground(table.getSelectionBackground());
            		label.setForeground(table.getSelectionForeground());            		
            	} else {
            		label.setForeground(table.getForeground());
        			label.setBackground(table.getBackground());
            	}
            	return label;
            } else {
            	JProgressBar progressBar = progressBars.get(file);
            	if(progressBar == null) {
            		progressBar = new JProgressBar();
            		progressBar.setBorder(new EmptyBorder(0, 5, 0, 5));
            		progressBars.put(file, progressBar);            		
            	}
            	progressBar.setValue(progress);
            	return progressBar;
            }            
        }
    }
